package cn.itcast.zuoyebang.fragment.tablefragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.zuoyebang.R;
import cn.itcast.zuoyebang.view.ViewPagerIndicator;
import cn.itcast.zuoyebang.view.ViewPagerIndicatorOnclickListener;


public class TableFragment extends Fragment {

    private ViewPagerIndicator indicator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_table, container, false);
        ViewPager imageVP = view.findViewById(R.id.vp_img);
        indicator = view.findViewById(R.id.indicator);
        indicator.setOnClickListener(new ViewPagerIndicatorOnclickListener() {
            @Override
            public void onClick(int position) {
                imageVP.setCurrentItem(position);//点击翻页
            }
        });


        //drawable图片变成ImageView
        List<View> viewList = new ArrayList<>();
        ImageView img = new ImageView(getContext());
        ImageView img2 = new ImageView(getContext());
        ImageView img3 = new ImageView(getContext());
        img.setBackgroundResource(R.drawable.banner_1);
        img2.setBackgroundResource(R.drawable.banner_2);
        img3.setBackgroundResource(R.drawable.banner_3);
        viewList.add(img);
        viewList.add(img2);
        viewList.add(img3);

        indicator.setCount(viewList.size());
        indicator.setCurrent(0);

        ImageAdapter imageAdapter = new ImageAdapter(viewList);
        imageVP.setAdapter(imageAdapter);

        //设置imageVP的监听事件
        imageVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicator.setCurrent(position);//当图片切换时返回页数
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return view;
    }

}